<?php
if(!$succ) {
    $cs=Yii::app()->getClientScript();
    $cs->registerCoreScript('jquery');
    $cs->registerScriptFile(SystemAsset::toUrl('jquery.tablednd.js'));
?>

<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($recipe); ?>

<div>份量<input type='text' name='servings' value='<?=$recipe->servings?>'></div>
<table id="ingredients-table" cellspacing="0" cellpadding="2">
<?php
if(empty($recipe->ingredients)) {
    $recipe->ingredients[]=new Ingredient();
    $recipe->ingredients[]=new Ingredient();
    $recipe->ingredients[]=new Ingredient();
}
foreach($recipe->ingredients as $k=>$ingredient) {
?>               

<tr id="<?=$k?>">
    <input type='hidden' name='ingredientsList[]' value='<?=$k?>'>
    <td>食材<input type='text' name='name<?=$k?>' value='<?=CHtml::encode($ingredient->name)?>'></td>
    <td>份量<input type='text' name='quanlity<?=$k?>' value='<?=CHtml::encode($ingredient->quanlity)?>'></td>    
    <td><a href="javascript:deleteRow('<?=$k?>')">删除</a></td>
</tr>            

<?php } ?>

</table>


<a href="javascript:appendRow();">点击新增行</a>

  <div style="text-align: center; margin-top: 20px; font-size: 21px;" class="submit">
    <?php echo CHtml::submitButton('保存'); ?>
  </div>
<?php echo CHtml::endForm(); ?>


<script type="text/javascript">
$(document).ready(function() {
    $('#ingredients-table').tableDnD();
});

appendRow.counter = 1000;

function appendRow()
{
    var i = appendRow.counter++;
    var str="<tr id='"+ i +"'><input type='hidden' name='ingredientsList[]' value='" + i + "'><td>食材<input type='text' name='name" + i +"'></td><td>份量<input type='text' name='quanlity" + i + "'></td> <td><a href='javascript:deleteRow(\'" + i + "\')'>删除</a></td> </tr>";
    $('#ingredients-table').append(str);
    $('#ingredients-table').tableDnDUpdate();
}

function deleteRow(rowId)
{
    rowId='#'+rowId;
    $(rowId).empty();        
    $('#ingredients-table').tableDnDUpdate();
}
</script>

<?php 
}
else {    
    $resultHtml=CJavaScript::quote($this->renderPartial('_ingredients', array('recipe'=>$recipe), true));
    echo CHtml::script("
        parent.setRecipeIngredients('$resultHtml');
        parent.$.fn.colorbox.close();
    ");
}
?>


